*******************************************************************************
* Specify city and years; load data
*******************************************************************************

local city = "fort_worth"

local y1 2012
local y2 2016
local y3 2020

use dataset_`city'_cross_pres, clear

*******************************************************************************
* Rescale explanatory variables
*******************************************************************************

foreach x in pctnhblack distancem {
	su `x'
	replace `x' = (`x'- r(min)) / (r(max) - r(min))
}
replace distancem = -distancem + 1

*******************************************************************************
* Time interactions
*******************************************************************************

tabulate year, gen(year_)
egen precinctyear = group(precinct year)
gen ltotalvotes = log(totalvotes)
gen pctnhblackXy2 = pctnhblack*year_2
gen pctnhblackX2020 = pctnhblack*year_3
gen distanceXy2 = distancem*year_2
gen distanceX2020 = distancem*year_3

label variable distancemi "Proximity to Shooting"
label variable pctnhblack "Share Black"
label variable pctnhblackXy2 "Share Black X 2016"
label variable pctnhblackX2020 "Share Black X 2020"
label variable distanceXy2 "Proximity to Shooting X 2016"
label variable distanceX2020 "Proximity to Shooting X 2020"

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear

eststo: reg pctdem pctnhblack pctnhblackXy2 pctnhblackX2020 ///
	distancem distanceXy2 distanceX2020 year_2 year_3 x_c y_c ///
	[aw=totalvotes], robust 
estadd local yfe = "X"
estadd local wtvote = "X"
estadd local clvote = ""
estadd local latlon = "X"
estadd local myse = "Robust"
estadd local myse2 = ""
	
foreach l in 1 2 5 {
	eststo: acreg pctdem pctnhblack pctnhblackXy2 pctnhblackX2020 ///
		distancem distanceXy2 distanceX2020 ///
		x_c y_c year_2 year_3 ltotalvotes, ///
		spatial latitude(y_c) longitude(x_c) distcutoff(`l') ///
		hac id(precinctyear) time(year) lagcutoff(0)
	estadd local slag = `l'
	estadd local yfe = "X"
	estadd local wtvote = ""
	estadd local clvote = "X"
	estadd local latlon = "X"
	estadd local myse = "HAC"
	estadd local myse2 = ""
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

esttab
	using "_output/table A26.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		drop(*_c ltot* year_2 year_3 _cons)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		stats(yfe wtvote clvote latlon myse slag N, 
			label(
				"Year Fixed Effects"
				"Weight by Total Votes Cast"
				"Control for Log(Total Votes Cast)"
				"Control for Latitude \& Longitude"
				"Standard Errors"
				"Distance Cutoff (Miles)"
				"Number of Precincts" 
			)
			fmt(%20.0f %20.0f))
		compress
		alignment(l)	
		gap
		;
			
#delimit cr	

*******************************************************************************
* End
*******************************************************************************
